package com.scorpio.goods.service.impl;

import com.scorpio.common.exception.CustomException;
import com.scorpio.common.service.impl.BaseServiceImpl;
import com.scorpio.goods.domain.GoodsSkuSpecEntity;
import com.scorpio.goods.domain.GoodsSpecEntity;
import com.scorpio.goods.enums.GoodsStatusEnum;
import com.scorpio.goods.mapper.GoodsSpecMapper;
import com.scorpio.goods.service.GoodsService;
import com.scorpio.goods.service.GoodsSkuSpecService;
import com.scorpio.goods.service.GoodsSpecService;
import com.scorpio.goods.vo.GoodsNumberVo;
import com.scorpio.redis.cachelock.RedisLock;
import com.scorpio.redis.cachelock.RedisLockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsSpecServiceImpl extends BaseServiceImpl<GoodsSpecMapper, GoodsSpecEntity> implements GoodsSpecService {

    @Autowired
    private RedisLock redisLock;

    @Autowired
    private GoodsSkuSpecService skuSpecService;

    @Autowired
    private GoodsService goodsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInventory(Long id, Integer change) {
        if(change == null || change <= 0){
            throw new CustomException("无效的参数");
        }
        String lockKey = "GoodsSpecServiceImpl_updateInventory:" + id;
        if (redisLock.lock(lockKey, 5)) {
            try {
                GoodsSpecEntity entity = baseDao.selectById(id);
                if (entity == null) {
                    throw new CustomException("商品不存在或已删除");
                } else if ((entity.getInventoryTotal() - change) < 0) {
                    throw new CustomException("商品不足，请重新下单");
                }
                int count = goodsService.updateInventory(entity.getGoodsId(), change);
                if (count == 0) {
                    throw new CustomException("操作失败");
                }
                return baseDao.updateInventory(id, -change, change);
            } finally {
                redisLock.unlock(lockKey);
            }
        }
        throw new RedisLockException();
    }

    @Override
    public GoodsSpecEntity selectByNumber(String number) {
        GoodsSpecEntity entity = baseDao.selectByNumber(number);
        if (entity == null) return null;
        entity.setSpecsName(getSpecName(entity));
        return entity;
    }

    @Override
    public List<GoodsSpecEntity> findSubmitShopInfo(List<GoodsNumberVo> list) {
        String[] numbers = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            GoodsNumberVo vo = list.get(i);
            numbers[i] = vo.getNumber();
        }
        List<GoodsSpecEntity> goodsSpecs = baseDao.selectByNumbers(numbers);
        if (list.size() != list.size()) {
            throw new CustomException("部分商品已下架,请刷新重新下单");
        }
        for (GoodsSpecEntity entity : goodsSpecs) {
            if (!GoodsStatusEnum.Status0.getValue().equals(entity.getStatus())) {
                throw new CustomException("部分商品已下架,请刷新重新下单");
            }
            for (GoodsNumberVo vo : list) {
                if (entity.getNumber().equals(vo.getNumber())) {
                    entity.setAmountTotal(vo.getAmountTotal());
                }
            }
            entity.setSpecsName(getSpecName(entity));
        }
        return goodsSpecs;
    }


    @Override
    public String getSpecName(GoodsSpecEntity entity) {
        List<GoodsSkuSpecEntity> list = skuSpecService.selectSkuBySpec(entity.getId());
        StringBuilder sb = new StringBuilder();
        for (GoodsSkuSpecEntity skuEntity : list) {
            if (sb.length() > 0) {
                sb.append("、");
            }
            sb.append(skuEntity.getSpecName());
        }
        return sb.toString();
    }
}
